package com.siemens.industrial.web.mvc.controller.admin;

import com.siemens.industrial.model.JobType;
import com.siemens.industrial.model.admin.SystemConfig;
import com.siemens.industrial.model.admin.Tenant;
import com.siemens.industrial.model.admin.User;
import com.siemens.industrial.utils.GlobalResponseCode;
import com.siemens.industrial.utils.JsonToOtherFormat;
import com.siemens.industrial.utils.RestResponse;
import com.siemens.industrial.utils.SocketClient;
import com.siemens.industrial.web.mvc.service.AuthService;
import com.siemens.industrial.web.mvc.service.WriteLogs;
import com.siemens.industrial.web.mvc.service.admin.SystemConfigService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * Created by qupen on 2016/9/22.
 */
@Controller
public class SystemConfigController {

    @Resource
    SystemConfigService systemConfigService;

    @Resource
    AuthService authService;

    @Resource
    WriteLogs writeLogs;

    @RequestMapping(value = "/admin/api/v1/config/logs", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> settingLogs(@RequestBody Map<String, Object> data) {
        systemConfigService.logsConfig(SystemConfig.LOGS_CONFIG_KEY, data);

        return RestResponse.create(GlobalResponseCode.SUCCESS).build();
    }

    @RequestMapping(value = "/admin/config/logs", method = RequestMethod.GET)
    public String settinglogs(ModelMap modelMap) {
        SystemConfig systemConfig = systemConfigService.getOneByConfigKey(SystemConfig.LOGS_CONFIG_KEY);
        if (systemConfig != null) {
            String value = systemConfig.getSystemValue();
            System.out.print(value);
            Map<String, Object> map = JsonToOtherFormat.buildJsonToMap(value);
            modelMap.put("logsInfo", map);
        }
        return "admin/logs_manage";
    }

    @RequestMapping(value = "/admin/config/email", method = RequestMethod.GET)
    public String settingMail(ModelMap modelMap) {
        SystemConfig systemConfig = systemConfigService.getOneByConfigKey(SystemConfig.Mail_CONFIG_KEY);
        if (systemConfig != null) {
            String value = systemConfig.getSystemValue();
            System.out.print(value);
            Map<String, Object> map = JsonToOtherFormat.buildJsonToMap(value);
            modelMap.put("mailInfo", map);
        }
        return "admin/mail_config";
    }

    @RequestMapping(value = "/admin/api/v1/config/mail", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> settingMail(@RequestParam(name = "mailConfig") String data) {
        systemConfigService.mailConfig(SystemConfig.Mail_CONFIG_KEY, data);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        User currentUser = (User) authService.currentUser();
        String content = currentUser.getUserName() + "于" + simpleDateFormat.format(new Date()) + "设置了邮件服务器。";
        writeLogs.writeLog(SystemConfig.LOG_USER_OPERATE_KEY, content, "platform_operate");
        return RestResponse.create(GlobalResponseCode.SUCCESS).build();
    }

    @RequestMapping(value = "/admin/api/v1/config/DBBackup", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> setDBBackup(@RequestBody Map<String, Object> map) {
        int i = systemConfigService.setDBBackup(SystemConfig.DBBACKUP_KEY, JsonToOtherFormat.buildJsonToString(map));
        if (i == 1) {
            SocketClient socketClient = new SocketClient();
            if (socketClient.sendRequest(JobType.DB_Backup_Plan_Update.toString())) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                User currentUser = (User) authService.currentUser();
                String content = currentUser.getUserName() + "于" + simpleDateFormat.format(new Date()) + "设置了备份计划。";
                writeLogs.writeLog(SystemConfig.LOG_USER_OPERATE_KEY, content, "database_operate");
                return RestResponse.create(GlobalResponseCode.SUCCESS).build();
            }
        }
        return RestResponse.create(GlobalResponseCode.ERROR_SETTING_DBBACKUP).build();
    }

    @RequestMapping(value = "/admin/api/v1/getDBBackupInfo", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getDBBackupInfo() {
        SystemConfig systemConfig = systemConfigService.getOneByConfigKey(SystemConfig.DBBACKUP_KEY);
        Map<String, Object> map = null;
        if (systemConfig != null) {
            map = JsonToOtherFormat.buildJsonToMap(systemConfig.getSystemValue());
        }
        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(map).build();
    }
}
